#!/bin/bash

echo "=== SM4算法验证（最终版）==="

# 1. 运行学长代码
echo "1. 学长代码结果："
./testsm4

echo ""
echo "2. GmSSL验证："

# 创建测试文件
echo -n -e '\x01\x23\x45\x67\x89\xab\xcd\xef\xfe\xdc\xba\x98\x76\x54\x32\x10' > plain.bin

echo "   明文: 0123456789ABCDEFFEDCBA9876543210"
echo "   密钥: 0123456789ABCDEFFEDCBA9876543210"
echo "   IV: 00000000000000000000000000000000"

# 使用CBC模式加密
echo ""
echo "3. GmSSL CBC模式加密："
gmssl sm4 -encrypt -cbc -key "0123456789ABCDEFFEDCBA9876543210" -iv "00000000000000000000000000000000" -in plain.bin -out cipher_cbc.bin

echo -n "   GmSSL CBC密文: "
xxd -p cipher_cbc.bin | tr -d '\n'
echo ""

# 使用CTR模式加密
echo ""
echo "4. GmSSL CTR模式加密："
gmssl sm4 -encrypt -ctr -key "0123456789ABCDEFFEDCBA9876543210" -iv "00000000000000000000000000000000" -in plain.bin -out cipher_ctr.bin

echo -n "   GmSSL CTR密文: "
xxd -p cipher_ctr.bin | tr -d '\n'
echo ""

echo "5. 学长代码预期密文: 681edf34d206965e86b3e94f536e4246"

# 解密验证
echo ""
echo "6. GmSSL解密验证："
gmssl sm4 -decrypt -cbc -key "0123456789ABCDEFFEDCBA9876543210" -iv "00000000000000000000000000000000" -in cipher_cbc.bin -out decrypted_cbc.bin

if cmp -s plain.bin decrypted_cbc.bin; then
    echo "   ✓ CBC模式解密验证成功"
else
    echo "   ✗ CBC模式解密验证失败"
fi

# 清理
rm -f plain.bin cipher_cbc.bin cipher_ctr.bin decrypted_cbc.bin

echo ""
echo "=== 验证完成 ==="
